
from pyeasyaccept.commands.InternalCommands import InternalCommands
from inspect import *

class CommandsDictionary:
    """
    This class represents a dictionary which keys are commands and values are arguments from that commands
    @author: Mikaela Maia
    """
    def __init__(self):
        """
        Constructor has an user facade like argument
        """
        self.internalCommands = InternalCommands()
        self.dynamic_choices = [];
        self.dic={}
        self.setDynamicChoices()
        
    def setUserAppFacade(self, userAppFacade):
        self.userAppFacade = userAppFacade
        self.loadUserAppCmds()
       
    def setDynamicChoices(self): 
        """
        This method sets the dynamic choices to auto complete
        """
        internalCommands = InternalCommands()       
        self.dynamic_choices = internalCommands.getCommandsStr()
        for  cmd in self.dynamic_choices:
             instance = internalCommands.getInternalCommandInstance(cmd)
             self.dic[cmd]= instance.getParametersNames()
        
                
    def loadUserAppCmds(self):
        commandsStr = dir(self.userAppFacade)

        for cmd in commandsStr:
            method = getattr(self.userAppFacade, cmd)
            
            if method != None and ismethod(method) and not cmd.startswith("__"):
                self.dynamic_choices.append(cmd)
                args = getargspec(method)[0]
                args.remove("self")
                self.dic[cmd] = args
        
                
    def getMethodArguments(self,method):
        """
        This method gets arguments from a method
        @param method: method's name
        @return: arguments from the method
        """ 
        if self.dic[method]!= None:
            return self.dic[method]
        return ""
    
    def getDynamicChoices(self):
        """
        This method gets dynamic choices from the auto complete
        @return: dynamic choices
        """
        
        return self.dynamic_choices
    
    def __str__(self):
        """
        This method returns the commands dictionary in a string form
        @return: commands dictionary in a string form
        """
        return self.dic.__str__()